<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            库内转移
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">器具条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描器具条码" id="ctBarCode" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">调入库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 调入库位" id="relWorkCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">源库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 源库位" id="workCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('1')"></i>
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <div style="background-color:white; height: 320px;width: 100%;margin-top:3%;border:solid 1px #D3D3D3;display:block"
            id="tableDiv">
            <table class="table" style='width:100%; font-size:14px;border: 1px solid white;border-collapse: collapse'  id="table">
             
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCellGid = ""; //源库位GID
    var workCellCode = ""; //源库位编码
    var workCellName = ""; //源库位名称
    var relWorkCellGid = ""; //调入库位GID
    var relWorkCellCode = ""; //调入库位编码
    var relWorkCellName = ""; //调入库位名称

    var scanRule = ""; //条码解析规则

    //初始化
    apiready = function () {

        //光标在调入库位
        $("#ctBarCode").focus();
        idSelector = "ctBarCode";

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }


    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1源库位
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCellfd(ret, idSelector, "1");


                //2：调入库位
            } else if (idSelector == 'relWorkCellCode') {

                $("#relWorkCellCode").val(ret);
                //查询库位
                getWorkCellfd(ret, idSelector, "2");

                //4：扫描条码
            } else if (idSelector == 'ctBarCode') {
                $("#ctBarCode").val(ret);
            }
        });
    }

    //5:源库位扫描校验
    function getWorkCellfd(code, inputId, flag) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCellfd";
        var data = {
            values: {
                flag: "out_kn",
                workCenterGid: "", //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);

                    //源库位
                    if (flag == "1") {
                        workCellGid = list[0].workCellGid;
                        workCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        workCellName = list[0].workCellName;
                        document.getElementById(inputId).value = workCellCode;
                        $("#ctBarCode").focus();
                        idSelector = "ctBarCode";

                        //调入库位
                    } else if (flag == "2") {

                        relWorkCellGid = list[0].workCellGid;
                        relWorkCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        relWorkCellName = list[0].workCellName;
                        document.getElementById(inputId).value = relWorkCellCode;
                        $("#workCellCode").focus();
                        idSelector = "workCellCode";
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#" + inputId).focus();
                    $("#" + inputId).select();
                }
            } else {
                alert("连接超时，请耐心等待");
            }
        });
    }

    //6：选择库位
    function goSelectWorkCell(workCellFlag) {
        var tm = $("#tm").val();
        api.openWin({
            name: 'uwmSelectWorkCellfd',
            url: 'uwmSelectWorkCellfd.html',
            pageParam: {
                flag: "out_kn",
                name: "uwmInTransfer-fd",
                tm: tm,
                workCenterGid: "",
                relWorkCellGid: relWorkCellGid, //调入库位
                workCellFlag: workCellFlag
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name, workCellFlag) {

        //源库位
        if (workCellFlag == "1") {

            workCellGid = id;
            workCellCode = code;
            workCellName = name;
            document.getElementById('workCellCode').value = code + ":" + name;

            //调入库位
        } else if (workCellFlag == "2") {

            relWorkCellGid = id;
            relWorkCellCode = code;
            relWorkCellName = name;
            document.getElementById('relWorkCellCode').value = code + ":" + name;
            $("#workCellCode").focus();
            var e = "workCellCode";
            idSelector = e;
        }
    }

    //7；PDA自带扫描回车事件-源库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCellfd(sm, "workCellCode", "1");
        }
    });

    //8；PDA自带扫描回车事件-调入库位
    $("#relWorkCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            //仓库编码
            $("#relWorkCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCellfd(sm, "relWorkCellCode", "2");
        }
    });

    //保存按钮
    function save() {
        //器具条码
        //var ctBarCode = $("#ctBarCode").val();

        //调入库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            api.toast({
                msg: '请选择/扫描 调入库位',
                duration: 3000
            });
            return;
        }

        var columnData = $('#table td:nth-child(1)').map(function () {
            return $(this).text();
        }).get();
       

        //器具条码非空校验
        if (columnData == "" || columnData == null || columnData == undefined) {
            api.toast({
                msg: '请扫码器具条码',
                duration: 3000
            });
            return;
        }


        //源库位和调入库位不能一样
        if (workCellGid != null && workCellGid != "" && workCellGid == relWorkCellGid) {
            api.toast({
                msg: '源库位和调入库位不能一样',
                duration: 3000
            });
            return;
        }
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmInTransferSaveqj";

        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCellGid: workCellGid, //源库位编码
                relWorkCellGid: relWorkCellGid, // 调入库位
                ctBarCode: columnData, //器具条码
                un: un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {

                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    //清空表单
                    $("#ctBarCode").val("");
                    $("#workCellCode").val("");
                    //清空源库位
                    workCellCode = "";
                    workCellGid = "";
                    workCellName = "";
                    api.toast({
                        msg: '保存成功',
                        duration: 3000
                    });

                    var list = myobj.data;

                    console.log(list);
                    var divStr ="";

                    if (list != null && list != undefined && list.length > 0) {
                        divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><th  style='border: 1px solid white;border-collapse: collapse;width:60%'>器具条码</th  style='border: 1px solid white;border-collapse: collapse;width:40%'><th>提示信息</th></tr>"
                        for (var i = 0; i < list.length; i++) {
                            divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><td id= 'tm' style='border: 1px solid white;border-collapse: collapse;width:60%'>" + list[i].barcode + "</td>";
                            if (list[i].code == "1") {
                                divStr += "<td style='background-color:red;border: 1px solid white;border-collapse: collapse;width:40%;color:white' >" + list[i].message + "</td></tr>"
                            } else {
                                divStr += "<td style='background-color:green;border: 1px solid white;border-collapse: collapse;width:40%;color:white'>" + list[i].message + "</td></tr>"
                            }
                        }
                        // $("$table").append(divStr);
                        $api.html($api.byId("table"), divStr);
                    }

                    // //遍历
                    // for (var key in map) {
                    //     $("#" + key).html(map[key]);
                    // }
                    // $("#message").html(myobj.message);
                    $("#ctBarCode").focus();
                    var e = "ctBarCode";
                    idSelector = e;
                } else {
                    $("#ctBarCode").val("");
                    $("#ctBarCode").select();
                    var e = "ctBarCode";
                    idSelector = e;
                    api.hideProgress();
                    //clearTabltDiv(myobj.message);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#ctBarCode").val("");
                $("#ctBarCode").select();
                idSelector = "ctBarCode";
                api.hideProgress();
                alert("连接超时，请耐心等待");
            }
        });
    }

     //
     document.addEventListener("keydown", function (e) {
        // 判断按下的键是否是回车键（键码为13）
        if (e && e.keyCode == 123) {
            console.log("Enter key pressed: event.keyCode=" + e.keyCode);
            // 在这里可以添加具体的逻辑代码
            doScanTm()
        }
    });

    function doScanTm() {
        var moduleUhfRfid = api.require('moduleUhfRfid');
        console.log(moduleUhfRfid);
        moduleUhfRfid.readRfid({
        }, function (ret, err) {
            console.log(JSON.stringify(ret));
            if (ret) {
                if (ret.code == "S") {
                    var list = eval(ret.detailJson);
                    //console.log(list);
                    var divStr = "";
                    if (list != null && list != undefined && list.length > 0) {
                        divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><th style='border: 1px solid white;border-collapse: collapsel;width:60%'>器具条码</th><th style='border: 1px solid white;border-collapse: collapse;width:40%'>删除条码</th></tr>"
                        for (var i = 0; i < list.length; i++) {
                            divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><td id= 'tm' style='border: 1px solid white;border-collapse: collapse;width:60%''>" + list[i].ASCII + "</td>" + "<td name = 'button' style='border: 1px solid white;border-collapse: collapse;width:40%'><div class='aui-btn aui-btn-info' onclick='deleteRow(this)'>删除</div></td></tr>"
                            // console.log("第" + (i + 1) + "行EPC:" + list[i].EPC);
                            // console.log("第" + (i + 1) + "行ASCII:" + list[i].ASCII);
                            // console.log("第" + (i + 1) + "行TID:" + list[i].TID);
                        }
                        // $("$table").append(divStr);
                        $api.html($api.byId("table"), divStr);
                    }
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    $(document).ready(function () {
        $('#table').on('click', '.deleteRowBtn', function () {
            $(this).closest('tr').remove(); // 删除包含按钮的那一行
        });
    });

    function deleteRow(button) {
        var row = button.parentNode.parentNode;
        // 获取表格的引用
        var table = document.getElementById("table").getElementsByTagName('tbody')[0];
        // 删除当前行
        table.removeChild(row);
    }


    //清空
    function clearTabltDiv(message) {
        $("#workCenterNameT").html("");
        $("#workCellNameT").html("");
        $("#relWorkCellNameT").html("");
        $("#businessTypeT").html("");
        $("#subBusinessTypeT").html("");
        $("#ctBarCodeT").html("");
        $("#qtyT").html("");
        $("#createIdT").html("");
        $("#createDateT").html("");
        $("#message").html(message);
    }

    //自动保存事件
    function saveAuto() {
        //器具条码
        var ctBarCode = $("#ctBarCode").val();

        //调入库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            return;
        }

        //箱码非空校验
        if (ctBarCode == "" || ctBarCode == null || ctBarCode == undefined) {
            return;
        }


        //源库位和调入库位不能一样
        if (workCellGid != null && workCellGid != "" && workCellGid == relWorkCellGid) {
            return;
        }

        save();
    }



    //器具条码监听回车事件
    $("#ctBarCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#ctBarCode").blur();
            }
        }
    });

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>